/**
 * This class is a subclass of the Hand class, and are used to model a hand of Quad.
 * It overrides getTopCard,isValid and getType method that it inherits from Hand class.
 *
 * @author Jiang Feiyu
 *
 */
public class Quad extends Hand{
    /**
     * Constructor for the Quad type hand. Calls the constructor of Hand super class.
     *
     * @param player Player who plays the hand
     * @param cards  List of card played by the player
     */
    public Quad(CardGamePlayer player, CardList cards) {
        super(player, cards);
    }
    /**
     * Returns the top card of the hand
     */
    public Card getTopCard() {
        this.sort();
        int numToReturn = 0;
        if(this.getCard(1).getRank()== this.getCard(4).getRank()) {
            numToReturn = 4;
        }
        else if(this.getCard(0).getRank() == this.getCard(3).getRank()) {
            numToReturn = 3;
        }
        switch (numToReturn) {
            case 3:
                return this.getCard(3);
            case 4:
                return this.getCard(4);
            default:
                return null;
        }
    }
    /**
     * Checks whether the hand is a Quad.
     */
    public boolean isValid() {
        if(this.size() == 5) {
            this.sort();
            // If the first three are same rank
            if((this.getCard(0).getRank() == this.getCard(1).getRank()) && (this.getCard(1).getRank() ==
                    this.getCard(2).getRank())){
                if ((this.getCard(2).getRank() == this.getCard(3).getRank())){
                    return true;
                } else {
                    return false;
                }
            }
            // Check if the last three are same rank
            else {
                if ((this.getCard(1).getRank() == this.getCard(2).getRank()) && (this.getCard(2).getRank() ==
                        this.getCard(3).getRank()) && (this.getCard(3).getRank() == this.getCard(4).getRank())){
                    return true;
                }else {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * Returns type of string.
     */
    public String getType() {
        return "Quad";
    }
}
